//
//  MallShopHeadView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "MallShopHeadView.h"
#import "MallShopCollectionViewCell.h"

@interface MallShopHeadView ()<UICollectionViewDelegate , UICollectionViewDelegateFlowLayout , UICollectionViewDataSource>

Strong UICollectionView *collectionView;

@end

@implementation MallShopHeadView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.collectionView];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , 113 / WIDTH_5S_SCALE * 2) collectionViewLayout:flowLayout];
        [_collectionView registerClass:[MallShopCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionView"];
        _collectionView.backgroundColor = getColor(@"ffffff");
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MallShopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionView" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (NSAttributedString *)getAttributeName:(NSString *)name{
    NSMutableAttributedString *attStr = [NSMutableAttributedString new];
    NSAttributedString *nameAtt = [[NSAttributedString alloc]initWithString:@"|" attributes:@{NSForegroundColorAttributeName:getColor(mainColor)}];
    [attStr appendAttributedString:nameAtt];
    [attStr appendAttributedString:[[NSAttributedString alloc] initWithString:name]];
    return attStr;
}

#pragma mark -  UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_getRootMallClick) {
        _getRootMallClick(indexPath.row);
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(SCREEN_WIDTH / 3 , 113 / WIDTH_5S_SCALE);
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.collectionView reloadData];
}


@end
